package com.hspedu.generic;

import java.util.*;

/**
 * @author DL5O
 * @version 1.0
 */
public class GenericExercise {
    public static void main(String[] args) {
        Student ywl = new Student("ywl", 21);
        Student zsj = new Student("zsj", 20);
        Student sky = new Student("sky", 19);

        //HashMap 遍历方式
//        System.out.println("====HashMap遍历方式====");
        HashMap<String, Student> map = new HashMap<String, Student>();
        /*
            public class HashMap<K,V>
         */
        map.put("ywl", ywl);
        map.put("zsj", zsj);
        map.put("sky", sky);
        System.out.println("====keySet====");


        //迭代器 EntrySet
        Set<Map.Entry<String, Student>> entries = map.entrySet();
        /*
             public Set<Map.Entry<K,V>> entrySet() {
                Set<Map.Entry<K,V>> es;
                return (es = entrySet) == null ? (entrySet = new EntrySet()) : es;
            }
         */
        Iterator<Map.Entry<String, Student>> iterator1 = entries.iterator();
        while (iterator1.hasNext()) {
            Map.Entry<String, Student> student = iterator1.next();
            System.out.println(student.getKey() + "-" + student.getValue());
        }

        //HashSet
        System.out.println("====HashSet====");
        HashSet<Student> students = new HashSet<>();
        students.add(ywl);
        students.add(zsj);
        students.add(sky);

        //迭代器遍历
        Iterator<Student> iterator = students.iterator();
        while (iterator.hasNext()) {
            Student student = iterator.next();
            System.out.println(student);
        }

        System.out.println("====增强for循环====");
        //for循环遍历
        for (Student student : students) {
            System.out.println(student);
        }

    }
}

class Student {
    public String name;
    public int age;

    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "姓名:" + name + ",年龄:" + age;
    }
}
